﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Sinacor.Infra.UI.Controls.SmartClient.DataGrid
{
    public class DataGridRow : Microsoft.Windows.Controls.DataGridRow, IItemContainerDragSource, IItemContainerDropTarget
    {
        public DataGridRow()
        {
            DragDropManager dragDropManager = new DragDropManager(this);
        }

        #region DataGrid
        public SinacorDataGrid DataGrid
        {
            get { return (SinacorDataGrid)this.GetType().GetProperty("DataGridOwner", System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(this, null); }
        }
        #endregion

        #region ApplyStyle

        #region Constantes
        public const string DataGridRowBusiness01 = "DataGridRowBusiness01";
        public const string DataGridRowBusiness02 = "DataGridRowBusiness02";
        public const string DataGridRowBusiness03 = "DataGridRowBusiness03";
        public const string DataGridRowBusiness04 = "DataGridRowBusiness04";
        public const string DataGridRowBusiness05 = "DataGridRowBusiness05";
        public const string DataGridRowBusiness06 = "DataGridRowBusiness06";
        public const string DataGridRowBusiness07 = "DataGridRowBusiness07";
        public const string DataGridRowBusiness08 = "DataGridRowBusiness08";
        public const string DataGridRowBusiness09 = "DataGridRowBusiness09";
        public const string DataGridRowBusiness10 = "DataGridRowBusiness10";
        public const string DataGridRowBusiness11 = "DataGridRowBusiness11";
        public const string DataGridRowBusiness12 = "DataGridRowBusiness12";
        public const string DataGridRowBusiness13 = "DataGridRowBusiness13";
        public const string DataGridRowBusiness14 = "DataGridRowBusiness14";
        public const string DataGridRowBusiness15 = "DataGridRowBusiness15";
        #endregion

        #region ApplyingStyleEvent
        public static readonly RoutedEvent ApplyingStyleEvent = SinacorDataGrid.ApplyingStyleEvent.AddOwner(typeof(DataGridRow));
        /// <summary>
        /// Evento disparado depois do inicio da aplicação de um estilo 
        /// </summary>
        public event ApplyStyleHandler ApplyingStyle
        {
            add { AddHandler(ApplyingStyleEvent, value); }
            remove { RemoveHandler(ApplyingStyleEvent, value); }
        }

        /// <summary>
        /// Dispara o evento <c>ApplyingStyle</c>
        /// </summary>
        /// <param name="index">Índice do item</param>
        /// <param name="item">Item</param>
        /// <returns>Estilo a ser aplicado</returns>
        protected virtual string OnApplyingStyle(int index, object item)
        {
            ApplyStyleEventArgs e = new ApplyStyleEventArgs(ApplyingStyleEvent, this, new ItemInfo(index, item));
            this.RaiseEvent(e);
            return e.Style;
        }
        #endregion

        #region ApplyStyle
        /// <summary>
        /// Dispara evento ApplyingStyle e aplica estilo na linha conforme o retorno
        /// </summary>
        public void ApplyStyle()
        {
            string style = this.OnApplyingStyle(this.GetIndex(), this.DataContext);
            if (string.IsNullOrEmpty(style))
                style = null;

            switch (style)
            {
                case DataGridRow.DataGridRowBusiness01:
                case DataGridRow.DataGridRowBusiness02:
                case DataGridRow.DataGridRowBusiness03:
                case DataGridRow.DataGridRowBusiness04:
                case DataGridRow.DataGridRowBusiness05:
                case DataGridRow.DataGridRowBusiness06:
                case DataGridRow.DataGridRowBusiness07:
                case DataGridRow.DataGridRowBusiness08:
                case DataGridRow.DataGridRowBusiness09:
                case DataGridRow.DataGridRowBusiness10:
                case DataGridRow.DataGridRowBusiness11:
                case DataGridRow.DataGridRowBusiness12:
                case DataGridRow.DataGridRowBusiness13:
                case DataGridRow.DataGridRowBusiness14:
                case DataGridRow.DataGridRowBusiness15:
                    // Aplicando estilo
                    this.SetResourceReference(StyleProperty, style);
                    break;

                case null:
                    // Limpando estilo
                    this.ClearValue(StyleProperty);
                    break;

                default:
                    // Estilo inválido
                    throw new InvalidOperationException(string.Format("Invalid row style '{0}'", style));
            }
        }
        #endregion

        #endregion

        #region IDragSource Members

        FrameworkElement IDragSource.GetDraggedContent()
        {
            return this;
        }

        #endregion

        #region IItemContainerDragSource Members

        bool IItemContainerDragSource.IsDragging
        {
            get { return this.IsSelected; }
        }

        IItemsControlDragSource IItemContainerDragSource.GetItemsControl()
        {
            return ItemsControl.ItemsControlFromItemContainer(this) as IItemsControlDragSource;
        }

        #endregion
    }
}
